var vnamePattern=/[\u4e00-\u9fa5\w]{2,10}/;
var phonePattern=/1[345678]\d{9}/;
var passwordPattern=/[\S]{6,12}/;
function timer(e) {
        if ((e)>0){
            console.info("倒计时："+e)
            return e;
        }else{
            return login.$data.sendCaptcha="发送手机号验证码";
        }
}
var login=new Vue({
    el:"#login",
    data:{
        accountDisplay:"block",
        phoneDisplay:"none",
        sendCaptcha:"发送手机号验证码",
        timing:15,
        sender:true,
        vname:"",
        password:"",
        phoneNumber:"",
        confirmPassword:"",
        captcha:"",
        vnameFlag:false,
        passwordFlag:false,
        phoneNumberFlag:false,
        confirmPasswordFlag:false,
        captchaFlag:false,
        checked:false,
        errorInfo:""
    },
    methods:{
        checkVname:function(){
            console.info("blur is working")
            if (vnamePattern.test(this.vname)){
                this.vnameFlag=true;
                this.errorInfo="";
            }else {
                this.vnameFlag=false;
                // alert("用户名以字母，数字，英文，中文，下划线组成，长度为2~10");
                this.errorInfo="用户名以字母，数字，英文，中文，下划线组成，长度为2~10";
            }

        },
        checkPassword:function(){
            if (passwordPattern.test(this.password)){
                this.passwordFlag=true;
                this.errorInfo="";
            }else {
                this.passwordFlag=false;
                // alert("密码需要为非空字符并且长度6到12");
                this.errorInfo="密码需要为非空字符并且长度6到12";
            }
        },
        checkPhoneNumber:function(){
            if (phonePattern.test(this.phoneNumber)){
                this.phoneNumberFlag=true;
                this.errorInfo="";
            }else {
                this.phoneNumberFlag=false;
                // alert("请输入正确的手机号码格式");
                this.errorInfo="请输入正确的手机号码格式";
            }
        },
        toDuanxin:function () {
            this.accountDisplay="none";
            this.phoneDisplay="block";
        },
        toAccount:function () {
            this.accountDisplay="block";
            this.phoneDisplay="none";
        },
        toQQ:function () {
            window.location.href="qqLogin.html?p=1";
        },
        send:function () {
            if (this.phoneNumberFlag&&this.phoneNumber!==null&&this.phoneNumber.trim()!==""){
                console.info("手机号："+this.phoneNumber)
                // var that=this;
                //发送验证码的请求
                axios({
                    method:"post",
                    url:"/venue/captcha/"+this.phoneNumber
                }).then(res=>{
                    console.info(res)
                    console.info(res.data.msg)
                    if (res.data.code===200){
                        this.sender=false;
                        this.timer();
                        // this.timing = window.setTimeout("timer(this.timing--)",1000);
                    }
                }).catch(err=>{
                    console.info(err)
                });

            }

        },
        timer:function(){
            var t = 15
            var timecount = setInterval(()=>{
                t--
                this.timing = t
                if(t<0){
                    clearInterval(timecount);
                    this.sender=true;
                }
            },1000)
        },
        submit:function (n) {
            //登录注册方式：用户名（1）手机号（2）QQ号（3）
            switch (n) {
                case 1:{
                    if (this.vname.trim()!==""&&this.password.trim()!==""){
                        axios({
                            method:"post",
                            url:"/venue/login/1",
                            data:{
                                "vname":this.vname,
                                "pwd":this.password
                            },
                            param:{
                                "rm":this.checked
                            }
                        }
                        ).then(e=>{
                            console.info(e.msg)
                            console.info(e)
                            if (e.data.code===200){
                                alert("登录成功")
                                window.location.href="homePage.html";
                            }
                        }).catch(err=>{
                            console.info(err)
                        })
                    }else{
                        return this.errorInfo="请正确输入数据再登录";
                    }
                }//case 1 end
                    break;
                case 2:{
                    if (this.phoneNumberFlag){
                        axios.post("/venue/login/2",{
                            "phoneNumber":this.phoneNumber,
                            "captcha":this.captcha
                        }).then(e=>{
                            console.info(e)
                            console.info(e.data.msg)
                            if (e.data.code===200){
                                alert("登录成功")
                                window.location.href="homePage.html";
                            }
                        }).catch(err=>{
                            console.info(err)
                        })
                    }
                }//case 2 end
                    break;
            }//switch end
        }
    }
});